package lj.courseinterect.dao;

import lj.courseinterect.dao.i.ExamInfoDao;
import lj.courseinterect.dao.i.StudentExamDao;
import lj.courseinterect.entity.AnswerInfo;
import lj.courseinterect.entity.ExamInfo;
import lj.courseinterect.entity.StudentExam;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Root;
import java.util.List;

public class JpaStudentExamDao extends JpaDao<StudentExam, Long> implements StudentExamDao{

    public JpaStudentExamDao() {
        super(StudentExam.class);
    }


    
    @Override
    @Transactional(readOnly = true)

    public List<StudentExam> findByExaId(String exaId) {
        final CriteriaBuilder builder = this.getEntityManager().getCriteriaBuilder();
        final CriteriaQuery<StudentExam> criteriaQuery = builder.createQuery(this.entityClass);

        Root<StudentExam> root = criteriaQuery.from(this.entityClass);
        Path<String> namePath = root.get("exaId");
        criteriaQuery.where(builder.equal(namePath, exaId));


        TypedQuery<StudentExam> typedQuery = this.getEntityManager().createQuery(criteriaQuery);
        List<StudentExam> studentExams= typedQuery.getResultList();

        if (studentExams.isEmpty()) {
            return null;
        }
        return studentExams;
    }

    @Override
    public List<StudentExam> findByStuId(String stuId) {
        final CriteriaBuilder builder = this.getEntityManager().getCriteriaBuilder();
        final CriteriaQuery<StudentExam> criteriaQuery = builder.createQuery(this.entityClass);

        Root<StudentExam> root = criteriaQuery.from(this.entityClass);
        Path<String> namePath = root.get("stuId");
        criteriaQuery.where(builder.equal(namePath, stuId));


        TypedQuery<StudentExam> typedQuery = this.getEntityManager().createQuery(criteriaQuery);
        List<StudentExam> studentExams= typedQuery.getResultList();

        if (studentExams.isEmpty()) {
            return null;
        }
        return studentExams;
    }

}
